let mysql = require("mysql");
let enquequeCount = 0
// let fs = require('fs')
let pool = mysql.createPool({
  connectionLimit: 40, //连接数为10
  // queueLimit: 10,
  host: "47.100.55.117",
  user: "root",
  password: "TY911221",
  //   database: "test"
  database: "video_yk"
});

export let db = (
  sqlstr: String,
  insertParams?: any[],
  option?: any
) => {
  return new Promise((res, rej) => {
    pool.getConnection((err, connection) => {
   
      if (err) {
        console.log("数据库报错", err);
      } else {
        connection.query(sqlstr, insertParams, (err, result) => {
          if (err) {
            console.log(err);
            
            rej(err);
          console.log("释放连接...", sqlstr);

          } else {
            
            res(result);

          }
        });
        // console.log("释放连接...", sqlstr); 

          connection.release(); //释放连接


      }

      // connection.release(); //释放连接
      // connection.end()

    });
    //打开连接 日志处理
    pool.on("connection", function (connection) {
      // console.log("打开连接", connection.threadId);

    });
    // 连接队列
    pool.on("enqueue", function (el) {
      // console.log("连接队列", pool._events.enqueue.length);

      // fs.writeFileSync("./连接至数据.js", JSON.stringify(pool), "utf-8");

    });
    //连接被释放
    pool.on('release', function (connection) {

      // console.log("释放连接...");

    });

    // //此处关闭连接池需逻辑调用
    // pool.end(function(err) {
    //   // all connections in the pool have ended
    //   console.log("数据可被关闭", err);
    // });
  });
};
// export = db;
